/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javamu;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;

/**
 *
 * @author Administrator
 */
public class StudentClass {
	private int age;
	private String name;
	
	private HashSet<CourseClass> courses;
	
	private HashSet<Integer> lnums = new HashSet<>();
	
	private ArrayList<Integer> anserQuestions = new ArrayList<>();
	
	public StudentClass(int age, String name){
		this.age = age;
		this.name = name;
		this.courses = new HashSet();
	}
	
	public void addCourse(CourseClass course){
		this.courses.add(course);
		System.out.println("ID:" + course.getID() + ",NAME:" + course.getName());
	}
	
	public void edtCourse(CourseClass olds, CourseClass news){
		this.courses.remove(olds);
		this.courses.add(news);
	}
	
	public void delCourse(CourseClass course){
		this.courses.remove(course);
	}
	
	//for方法
	public void getCourses(){
		for (CourseClass course : courses) {
			System.out.println(course.getID() + ":" + course.getName());
		}
	}
	
	//迭代器方法
	public void getCoursesByIterator(){
		Iterator itr = this.courses.iterator();
		while (itr.hasNext()) {
			CourseClass course = (CourseClass)itr.next();
			System.out.println(course.getID() + ":" + course.getName());
		}
	}
	
	//forEach方法
	public void getCourses2(){
		courses.stream().forEach((course) -> {
			System.out.println(course.toString());
		});
	}
	
	public void addLnum(int nums){
		this.lnums.add(new Integer(nums));
		System.out.println(this.lnums.toString());
	}
	
	public void addAnserQuestion(int qnum){
		this.anserQuestions.add(qnum);
	}
	
	public void addAnserQuestion(int index, int qnum){
		this.anserQuestions.add(index, qnum);
	}
	
	public void edtAnserQuestion(int index, int qnum){
		this.anserQuestions.set(index, qnum);
	}
	
	public void delAnserQuestion(int index){
		this.anserQuestions.remove(index);
	}
	
	public void delAnserQuestion(Integer qnum){
		this.anserQuestions.remove(qnum);
	}
	
	public void addAllAnserQuestion(){
		Integer[] qnums = {111, 222, 333};
		this.anserQuestions.addAll(Arrays.asList(qnums)); //将qnums以ArrayList形式返回
		//this.anserQuestions.addAll(qnums);
	}
	
	public String getAnserQuestionS(){
		return this.anserQuestions.toString();
	}
	
	public boolean hasAnserQuestion(Integer qnum){
		return this.anserQuestions.contains(qnum);
	}
	
	public boolean hasAnserQuestion(int qnum){
		return this.anserQuestions.contains(qnum);
	}
	
	@Override
	public String toString(){
		return "AGE:" + this.age + ",NAME:" + this.name;
	}

	@Override
	public boolean equals(Object obj){
		if(this == obj){
			return true;
		}
		if(obj == null){
			return false;
		}
		if(obj instanceof StudentClass){
			StudentClass stobj = (StudentClass)obj;
			if(this.age == stobj.age && this.name.equals(stobj.name)){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	@Override
	public int hashCode() {
		int hash = 3;
		hash = 29 * hash + this.age;
		hash = 29 * hash + Objects.hashCode(this.name);
		hash = 29 * hash + Objects.hashCode(this.courses);
		hash = 29 * hash + Objects.hashCode(this.lnums);
		hash = 29 * hash + Objects.hashCode(this.anserQuestions);
		return hash;
	}
}
